package com.aisino.wxkp.action;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import javax.mail.Flags.Flag;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.aisino.wxkp.common.basic.WxkpBaseAction;
import com.aisino.wxkp.common.config.Constants;
import com.aisino.wxkp.common.date.DateFormate;
import com.aisino.wxkp.common.entity.DecrySmCAInfo;
import com.aisino.wxkp.common.entity.FpWxFpmx;
import com.aisino.wxkp.common.entity.JsAeroInfoReturnException;
import com.aisino.wxkp.common.entity.JsonRespon;
import com.aisino.wxkp.common.enums.RCodeEnums;
import com.aisino.wxkp.common.enums.ReqQyxx;
import com.aisino.wxkp.common.fpkj.dto.B_ReturnStateInfo;
import com.aisino.wxkp.common.fpkj.dto.InterfaceXML;
import com.aisino.wxkp.common.fpkj.dto.JshxTblFpcx;
import com.aisino.wxkp.common.fpkj.dto.JshxTblFpkjFptxx;
import com.aisino.wxkp.common.fpkj.dto.JshxTblFpkjFpxxNew;
import com.aisino.wxkp.common.util.Base64G;
import com.aisino.wxkp.common.util.HttpReqHelper;
import com.aisino.wxkp.common.util.HxJSONUtils;
import com.aisino.wxkp.common.util.InvParseO2X;
import com.aisino.wxkp.common.util.InvParseX2O;
import com.aisino.wxkp.common.util.ReadFromFile;
import com.aisino.wxkp.common.util.StringUtil;
import com.aisino.wxkp.service.WXKPService;
import com.aisino.wxkp.service.impl.WXKPServiceImpl;
import com.aisino.xstream.helper.Base64Helper;
import com.thoughtworks.xstream.XStream;

public class WXInvInfoAction extends WxkpBaseAction{
	WXKPService wxkpInvService = new WXKPServiceImpl();
	private static final Logger LOGGER = Logger.getLogger(WXInvOpenAction.class);
	public volatile boolean flag = false;

	public void WXKP(HttpServletRequest request,
			  HttpServletResponse response) throws ServletException, IOException {
		try {
			request.setCharacterEncoding("UTF-8");
			String nsrsbh = request.getParameter("xfsh");					// IC 企业税号      
			String fpqqlsh = request.getParameter("id");				// ID 发票流水号
			String ghfmc = Base64Helper.decode(request.getParameter("ghfmc").replace(" ", "+"));				// 购货方名称
			String xhfmc = Base64Helper.decode(request.getParameter("xhfmc").replace(" ", "+"));				// 销货方名称
			String kphjje = request.getParameter("kphjje");			    // 开票合计金额
			System.out.println(ghfmc+xhfmc);
			String DecrySmCA = Base64G.decode2(postClServerRequest("", Constants.TransferServicePr_queryGlobal_url+"?nsrsbh="+nsrsbh+""));
			JshxTblFpcx fpcxObj = new JshxTblFpcx();					
			XStream xStream = new XStream();
			xStream.alias("root", DecrySmCAInfo.class);					
			DecrySmCAInfo caInfo = (DecrySmCAInfo) xStream.fromXML(DecrySmCA);
			
			JsonRespon hasfp = wxkpInvService.validHasOpenInv(nsrsbh,fpqqlsh);
			
			//开具则返回发票信息，代码9999
			if (RCodeEnums.E9999.getCode().equals(hasfp.getCode())) {
			Object obj_t = hasfp.getData();
			if (obj_t != null) {
				JshxTblFpcx obj = (JshxTblFpcx) obj_t;
				JshxTblFpkjFpxxNew ykfp = obj.getYkfp();
				JshxTblFpkjFptxx fptxx = obj.getFptxx();
				StringBuffer fpdurl = new StringBuffer();
//				System.out.println("发票代码=="+ykfp.getFpDm()+"发票号码=="+ykfp.getFpHm()+"请求流水号=="+ykfp.getFpqqlsh());
				fpdurl.append("?fpdm=").append(
						Base64Helper.encode(ykfp.getFpDm()));
				fpdurl.append("&fphm=").append(
						Base64Helper.encode(ykfp.getFpHm()));
				fpdurl.append("&qqlsh=").append(
						Base64Helper.encode(ykfp.getFpqqlsh()));
				ykfp.setPdfUrl(Constants.DOWN_PDF_URL_ROOT
						+ fpdurl.toString());
				if (ykfp != null) {
					if (!fpqqlsh.equals(ykfp.getFpqqlsh())) {
						throw new JsAeroInfoReturnException("9998",
								"类型校验不一致!");// 已开具页面
					}
				ykfp.setByzd1("/invoice/img/"+nsrsbh+".jpg");
				fpcxObj.setYkfp(ykfp);
				fpcxObj.setFptxx(fptxx);
				
				//存库
				FpWxFpmx wxfp = new FpWxFpmx(ykfp.getKpy(), ykfp.getFpqqlsh(),  ykfp.getReturncode(),  ykfp.getReturnmessage(),String.valueOf(ykfp.getFpbid()), ykfp.getFpDm(), ykfp.getFpHm(), ykfp.getGhfsh(), ykfp.getGhfMc(), String.valueOf(ykfp.getKphjzje()),DateFormate.getFormatDate(ykfp.getKprq(),"yyyy-MM-dd HH:mm:ss"));
				String jsonStr = HxJSONUtils.getJSONString(wxfp);
				String resuleString = HttpReqHelper.postClServerRequest(Base64G.encode2(jsonStr),Constants.TransferServicePro_save);
//				System.out.println("存库信息=="+Base64G.decode2(resuleString));
				//跳转
				request.setAttribute("invoiceInfo", fpcxObj);
				request.getRequestDispatcher("/test/reqFpkj.jsp").forward(request,response);
						}
					}
			}else{	
				response.setCharacterEncoding("UTF-8");
				request.setCharacterEncoding("UTF-8");
				JshxTblFpkjFptxx fpttxx = new JshxTblFpkjFptxx();
				fpttxx.setGhfmc(ghfmc);
				fpttxx.setXhfmc(xhfmc);
				fpttxx.setKphjje(kphjje);
				fpttxx.setByzd1("/invoice/img/"+nsrsbh+".jpg");
				request.setAttribute("invoiceInfo", fpttxx);
				request.getRequestDispatcher("/test/reqFpsq.jsp").forward(request,response);
				}
		} catch (JsAeroInfoReturnException e) {
			try {
				request.setAttribute("errorcode", e.getCode());
				request.setAttribute("errorinfo", e.getMessage());
				request.getRequestDispatcher("error.jsp").forward(request,
						response);
				// response.sendRedirect("error.jsp");
			} catch (Exception e1) {
				// response.sendRedirect("500.jsp");
			}
			return;
		} catch (Exception e) {
			e.printStackTrace();
			//	response.sendRedirect("500.jsp");
		}
	}
	public static void main(String[] args) {
		try {
		String reqStr = ReadFromFile.readFileByLinesToString("d:/test.xml");
		System.out.println("请求报文=="+reqStr);
		String result = postClServerRequest(reqStr, "http://njxhsd.3322.org:14049/einvpf/service/einvSafePtCall?wsdl");
		System.out.println("返回报文=="+result);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
